import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/sales/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询列表'
});
const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询详情'
});
const getAddDesc = formatMessage({
    id: 'api.getAdd',
    defaultMessage: '新增'
});
const getModifyDesc = formatMessage({
    id: 'api.getModify',
    defaultMessage: '修改'
});
const approveDesc = formatMessage({
    id: 'api.approveDesc',
    defaultMessage: '审核'
});
const submitDesc = formatMessage({
    id: 'api.submitDesc',
    defaultMessage: '提交'
});
const getAbandonDesc = formatMessage({
    id: 'api.getAbandon',
    defaultMessage: '作废'
});
const saleOrderDesc = formatMessage({
    id: 'api.saleOrderDesc',
    defaultMessage: '查询销售订单'
});
const letterOfCreditDesc = formatMessage({
    id: 'api.letterOfCreditDesc',
    defaultMessage: '查询信用证'
});
const exportDataDesc = formatMessage({
    id: 'api.exportDataDesc',
    defaultMessage: '导出'
});
const getMarketDepartmentDesc = formatMessage({
    id: 'api.message.getMarketDepartmentDesc',
    defaultMessage: '查询大区'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/saleConfirmationCbu/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/saleConfirmationCbus?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询详情
export const getDetail = id => request(`${baseUrl}/saleConfirmationCbus/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 新增
export const newOrder = data => request(`${baseUrl}/saleConfirmationCbus`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAddDesc));

// 修改
export const modifyOrder = (id, data) => request(`${baseUrl}/saleConfirmationCbus/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getModifyDesc));

// 审核不通过
export const rejectData = (id, data) => request(`${baseUrl}/saleConfirmationCbus/${id}/reject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));

// 审核通过
export const approveData = (id, data) => request(`${baseUrl}/saleConfirmationCbus/${id}/pass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));

// 提交
export const submitData = (id, data) => request(`${baseUrl}/saleConfirmationCbus/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDesc));

// 作废
export const abandonData = (id, rowVersion) => request(`${baseUrl}/saleConfirmationCbus/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(
        rowVersion
    )
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));

// CBU销售订单查询
export const saleOrderQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/cbuSaleOrders/forSaleConfirmation?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, saleOrderDesc));
};

// CBU销售订单清单查询
export const saleOrderDetails = (id, pageOption) => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/cbuSaleOrders/${id}/forSaleConfirmation?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, saleOrderDesc));
};

// 信用证
export const letterOfCreditQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/letterOfCredits/forSaleConfirmation?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, letterOfCreditDesc));
};

// 导出
export const exportData = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/saleConfirmationCbus/export?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDataDesc));
};
// 查询大区
export const getMarketDepartment = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};

// 查询国家
const getAreasDesc = formatMessage({
    id: 'api.message.getAreasDesc',
    defaultMessage: '查询国家'
});
import {areaKind} from '../Enum';
export const getAreas = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/query?type=${areaKind.国家}&${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};

// 查询大区
export const getMarketDepartmentSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};
export const getAreasSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};

// 导出
export const printData = id => request(`${baseUrl}/saleConfirmationCbus/${id}/print`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDataDesc));
